package com.cz.run.config.mvc;

import com.cz.run.beans.base.ReturnDTO;
import com.cz.run.enums.SystemCodeEnum;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: TODO()
 * @auther:CAIYJ
 * @Date:2018/5/15 15:15
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger loggger = LogManager
            .getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ReturnDTO<?> defaultErrorHandler(HttpServletRequest req, Exception e)
            throws Exception {
        loggger.error(
                SystemCodeEnum.http_status_internal_server_error.getErrorMsg(),
                e);
        if (e instanceof NoHandlerFoundException) {
            return ReturnDTO.fail(SystemCodeEnum.http_status_not_found);
        }
        return ReturnDTO.fail(SystemCodeEnum.http_status_internal_server_error);
    }
}
